﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Coolite.Ext.Web;
using System.Configuration;
using System.Xml;
using System.Text;
using System.Xml.XPath;

namespace PeisongWeb.admin
{
    public partial class reportBusinessList : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("DC", 1))
                {
                    
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        protected bool hasSpecialPermission()
        {
            bool hasPermission = false;

            string ids = ConfigurationManager.AppSettings["SpecialPermission"];
            string[] adminIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string item in adminIds)
            {
                if (item == logic.sysAdmin.AdminID.ToString())
                {
                    hasPermission = true;
                    break;
                }
            }

            return hasPermission;
        }

        private void rptBind()
        {
            string where = "";
            DataSet ds = DAL.reportBusiness.list(where, "createdt desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void bt_create_click(object sender, EventArgs e)
        {
            string result = createResult("1");
            if (result == "0" || result == "-1" || result == "-2")
            {
                if (result == "0")
                {
                    ScriptManager1.AddScript("alert('当前没有提货单信息');");
                }
                else
                {
                    ScriptManager1.AddScript(string.Format("alert('{0}');", result == "-1" ? "未到生成时间，请点击使用预览功能。" : "当天提货单已生成，不能再次生成。"));
                }
            }
            else
            {
                ScriptManager1.AddScript("alert('生成成功');");
                sendSMSWindow.Show();
                // 读取短信模板
                readFromTemplate();
            }
        }

        private string createResult(string flag)
        {
            int adminid = logic.sysAdmin.AdminID;
            if (flag != "0" && flag != "1")
                flag = "0";
            string re_str = "";
            if (flag == "1")
            {
                re_str = DAL.reportBusiness.chkCreate();
                if (re_str != "")
                {
                    return re_str;
                }
            }
            DataTable dt = DAL.reportBusiness.create(adminid, flag,DateTime.Now);
            if (dt.Rows.Count == 0)
            {
                re_str = "0";//"当前没有提货单信息";
            }
            else
            {               
                re_str ="1";// "生成成功.";
            }
            return re_str;
        }

        private void readFromTemplate()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/app_resource/EmailTemplate/sms_template.xml"));

            XmlNodeList nl = doc.GetElementsByTagName("phone");
            string content = doc.GetElementsByTagName("content")[0].InnerText;

            StringBuilder phones=new  StringBuilder();
            foreach (XmlNode item in nl)
            {
                phones.AppendFormat("{0},", item.InnerText);
            }

            txtPhones.Text = phones.ToString();

            StringBuilder conteTemplate = new StringBuilder(content);
            DataRow dr=DAL.reportBusiness.getSMSData();
            conteTemplate.Replace("#date#", string.Format("{0}月{1}日", DateTime.Now.Month, DateTime.Now.Day));
            conteTemplate.Replace("#saleAmount#", string.Format("{0:F2}", dr["SaleAmount"]));
            conteTemplate.Replace("#supplyAmount#", string.Format("{0:F2}", dr["SupplyAmount"]));
            conteTemplate.Replace("#gpm#", string.Format("{0:F2}", dr["GPM"]));
            txtContent.Text = conteTemplate.ToString();
        }

        // 发送短信
        protected void btnSendSMS_Click(object sender, EventArgs e)
        {
            string phones = txtPhones.Text;
            string content = txtContent.Text;
            int adminId = logic.sysAdmin.AdminID;
            string url = Request.Url.ToString();
            logic.sysAdmin.SendSMS(phones,content,adminId,url);

            Library.Script.ClientMsg("发送成功!");
            rptBind();
        }
    }
}
